import React, { useState, useEffect } from 'react';
import { Table, message } from 'antd';
import { getBorrowList } from '../../services/api';

const BorrowList = () => {
  const [loading, setLoading] = useState(false);
  const [borrowList, setBorrowList] = useState([]);
  const [pagination, setPagination] = useState({
    current: 1,
    pageSize: 8,
    total: 0,
  });

  const columns = [
    {
      title: '借用ID',
      dataIndex: 'borrowId',
      key: 'borrowId',
    },
    {
      title: '借用人',
      dataIndex: 'uname',
      key: 'uname',
    },
    {
      title: '所属部门',
      dataIndex: 'depName',
      key: 'depName',
    },
    {
      title: '资产名称',
      dataIndex: 'assetName',
      key: 'assetName',
    },
    {
      title: '借用状态',
      dataIndex: 'borrowStatus',
      key: 'borrowStatus',
    },
    {
      title: '借用数量',
      dataIndex: 'borrowNum',
      key: 'borrowNum',
    }
  ];

  const fetchBorrowList = async () => {
    try {
      setLoading(true);
      const response = await getBorrowList();
      if (response.code === 200 && Array.isArray(response.data)) {
        setBorrowList(response.data);
        setPagination(prev => ({
          ...prev,
          total: response.data.length
        }));
      } else {
        message.error(response.message || '获取借用记录失败');
      }
    } catch (error) {
      console.error('获取借用记录失败:', error);
      message.error('获取借用记录失败');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchBorrowList();
  }, []);

  const handleTableChange = (newPagination) => {
    setPagination(newPagination);
  };

  return (
    <div style={{ padding: '24px' }}>
      <Table
        columns={columns}
        dataSource={borrowList}
        rowKey="borrowId"
        pagination={pagination}
        onChange={handleTableChange}
        loading={loading}
      />
    </div>
  );
};

export default BorrowList; 